package jfoo;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.UIManager;

import jfoo.detailsTable.DetailsTable;
import jfoo.detailsTable.DetailsTableModel;
import jfoo.playlist.PlaylistList;
import jfoo.playlist.PlaylistListModel;
import jfoo.playlist.PlaylistScroll;
import database.Database;

public class Window extends JFrame {
	PlaylistListModel playlistListModel;
	PlaylistList playlistList;
	ControlerWindow ecouteurFermer;

	public Window() {
		super();

		try {

			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

		} catch (Exception ex) {
		}

		// setUIFont(new javax.swing.plaf.FontUIResource("Serif",Font.PLAIN,7));

		this.setTitle("Jesuifoo");
		ImageIcon logo = new ImageIcon("img/icon.png");
		this.setIconImage(logo.getImage());
		this.setResizable(true);
		// this.setLocationRelativeTo(null);

		this.setPreferredSize(new Dimension(1000, 600));
		this.setSize(new Dimension(1000, 600));
		Database database = Database.getInstance();
		Preferences preferences = Preferences.getInstance();

		DetailsTableModel detailModel = new DetailsTableModel();

		ControlCenter controlCenter = new ControlCenter(this);
		ecouteurFermer = new ControlerWindow(this, controlCenter);
		this.addWindowListener(ecouteurFermer);

		playlistListModel = new PlaylistListModel(controlCenter);

		playlistList = new PlaylistList(playlistListModel, detailModel);
		DetailsTable tableDetail = new DetailsTable(detailModel, controlCenter,
				playlistList);
		PlaylistScroll playlistScroll = new PlaylistScroll(playlistList);

		// Playlist playlist = new Playlist("waxxy");
		// try {
		// playlist.add((new MP3(new File("TOM.mp3"))));
		// playlist.add((new MP3(new File("broken.mp3"))));
		// } catch (IOException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
		//
		// playlistList.addPlaylist(playlist);

		JPanel rightPanel = new JPanel();
		rightPanel.setLayout(new BorderLayout());

		VueDetail vueDetail = new VueDetail(tableDetail);
		SearchPanel searchPanel = new SearchPanel(this);
		rightPanel.add(vueDetail, BorderLayout.CENTER);
		rightPanel.add(searchPanel, BorderLayout.NORTH);

		JPanel leftPanel = new JPanel();
		leftPanel.setLayout(new BorderLayout());
		leftPanel.add(controlCenter, BorderLayout.NORTH);
		leftPanel.add(playlistScroll, BorderLayout.CENTER);

		leftPanel.setMaximumSize(new Dimension(100, 80));
		creationDuMenu();
		this.getContentPane().setLayout(new BorderLayout());
		this.getContentPane().add(leftPanel, BorderLayout.WEST);
		this.getContentPane().add(rightPanel, BorderLayout.CENTER);
		
		this.pack();
		this.setVisible(true);
	}

	public PlaylistList getPlaylistList() {
		return playlistList;
	}

	public static void setUIFont(javax.swing.plaf.FontUIResource f) {
		//
		// sets the default font for all Swing components.
		// ex.
		// setUIFont (new javax.swing.plaf.FontUIResource
		// ("Serif",Font.ITALIC,12));
		//
		java.util.Enumeration keys = UIManager.getDefaults().keys();
		while (keys.hasMoreElements()) {
			Object key = keys.nextElement();
			Object value = UIManager.get(key);
			if (value instanceof javax.swing.plaf.FontUIResource)
				UIManager.put(key, f);
		}
	}

	public void creationDuMenu() {

		JMenuBar barreMenu = new JMenuBar();
		this.setJMenuBar(barreMenu);

		// CREATION DU MENU FICHIER
		JMenu menuFichier = new JMenu("Fichier");
		barreMenu.add(menuFichier);

		JMenuItem itemPreferences = new JMenuItem("Preferences");
		JMenuItem itemExit = new JMenuItem("Exit");

		menuFichier.add(itemPreferences);
		menuFichier.add(itemExit);

		menuFichier.getPopupMenu().setLightWeightPopupEnabled(false);

		itemPreferences.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				PreferencesDialog dialog = new PreferencesDialog(Window.this);
				// Popup popup = fact.getPopup(Window.this,new
				// PreferencesDialog(popup),(int)(Window.this.getX() +
				// Window.this.getSize().getWidth()/2),(int)(Window.this.getX()
				// + Window.this.getSize().getHeight()/2));
				// popup.show();

			}
		});

		// http://www.java2s.com/Tutorial/Java/0240__Swing/JDialogisspecifythatpressingtheEscapekeycancelsthedialog.htm

		itemExit.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				Window.this.ecouteurFermer.windowClosing(null);
			}
		});

		}

	public PlaylistListModel getPlaylistListModel() {
		return playlistListModel;

	}

}
